package com.zjc;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ClassForName {


    public static Object execute(String className, String paramClass, String paramJson, ClassLoader classLoader) {
        try {
            Class<?> r = Class.forName(className, false, classLoader);

            Object ri = r.newInstance();

            String[] pTypes = paramClass.split(",");
            String[] pts = paramJson.split(",");

            Class[] paramTypes = new Class[pTypes.length];
            Object[] params = new Object[pTypes.length];

            for (int i = 0; i < pTypes.length; i++) {
                Class<?> ct = Class.forName(pTypes[i]);
                paramTypes[i] = ct;
                if (ct == String.class) {
                    params[i] = pts[i];
                }

            }

            Method rm = r.getDeclaredMethod("reflexMethod", paramTypes);

            Object rs = rm.invoke(ri, params);
            return rs;

        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}


